from flask import url_for
from flask_login import current_user
from werkzeug.utils import redirect


class AuthMixin(object):
    def is_accessible(self):
        if current_user.is_authenticated:
            try:
                if hasattr(current_user, "is_super_admin"):
                    return True
            except:
                return False
        return False

    def _handle_view(self, name, **kwargs):
        if not self.is_accessible():
            if not current_user.is_authenticated:
                return redirect("/admin/login")
            return redirect(url_for("dashboard_bp.home"))
